﻿package com.dbm.model;

import java.io.UnsupportedEncodingException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
 * Base64加密解密
 * @author wqying
 *
 */
public class Base64Model {
	
	//base64解密
	public String deCodeBase64(String str){
		 byte[] b = null;  
	     String result = null;  
	     if (str != null) {  
	         BASE64Decoder decoder = new BASE64Decoder();  
	         try {  
	             b = decoder.decodeBuffer(str);  
	             result = new String(b, "utf-8");  
	         } catch (Exception e) {  
	             e.printStackTrace();  
	         }  
	     }  
	     return result;  
	}
	//base64加密，通过urlencode编码。
	public String EncodeByBase64(String str){
		 byte[] b = null;  
	        String s = null;  
	        try {  
	            b = str.getBytes("utf-8");  
	        } catch (UnsupportedEncodingException e) {  
	            e.printStackTrace();  
	        }  
	        if (b != null) {  
	            s = new BASE64Encoder().encode(b);  
	        }  
	        return s; 
	}
	//base64加密，通过urlencode编码。
	public String UrlEncodeByBase64(String str){
		String ret = null;
		ret = new BASE64Encoder().encode(str.getBytes());
		String result = null;
		try {
			String mytext = java.net.URLEncoder.encode(ret,"utf-8");
			result = mytext;
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		return result;
	}
}
